﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier_Utils;
using Cashier.utils;

namespace Cashier.purchase
{
    public partial class RepurchaseGoodsListControl : UserControl, IDataGridViewData, IPrinter
    {
        private MainForm mainform;
        private static RepurchaseGoodsListControl repurchaseGoodsListControl;
        private BillRepurchaseGoodsService repurchaseGoodsService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        private RepurchaseGoodsListControl(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dataGridView1.ImplementsInstance = this;
            this.repurchaseGoodsService = new BillRepurchaseGoodsService();

            Button[] buttons = { button_query };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 获取RepurchaseGoodsListControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="repurchaseno"></param>
        /// <returns></returns>
        public static RepurchaseGoodsListControl GetRepurchaseGoodsListControl(MainForm mainform, String repurchaseno)
        {
            if (repurchaseGoodsListControl == null)
                repurchaseGoodsListControl = new RepurchaseGoodsListControl(mainform);
            if (!String.IsNullOrEmpty(repurchaseno))
                repurchaseGoodsListControl.textbox_repurchaseno.Text = repurchaseno;

            repurchaseGoodsListControl.RefreshDataGridViewData();
            return repurchaseGoodsListControl;
        }

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            return;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("barcode", "商品条码", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("category_name", "分类", true, 6, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_name", "名称", true, 18, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_norm", "规格", true, 6, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("price_cost", "进货单价", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("discount", "折扣率", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("price_disc", "折后单价", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("renumbers", "退货数量", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("owe_total", "小计", true, 6, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("supplier_name", "供货商", true, 8, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("repurchase_no", "退货单号", true, 12, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("note", "备注", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String enterpreseNo = mainform.Admin.EnterpriseNo;
                String barcode = textbox_barcode.Text.Trim();
                String goods_name = textbox_goodsname.Text.Trim();
                String category_name = textbox_categoryname.Text.Trim();
                String repurchaseno = textbox_repurchaseno.Text.Trim();
                String adminUsername = mainform.UsernameForQuery;
                String startdate = String.Empty;
                String enddate = String.Empty;
                if (dateTimePicker_startdate.Checked && dateTimePicker_enddate.Checked &&
                    dateTimePicker_enddate.Value.CompareTo(dateTimePicker_startdate.Value) >= 0)
                {
                    startdate = dateTimePicker_startdate.Value.ToShortDateString();
                    enddate = dateTimePicker_enddate.Value.ToShortDateString();
                }

                DataTable dt = this.repurchaseGoodsService.GetBillRepurchaseGoodsTable(pageindex, pagesize, enterpreseNo, string.Empty, repurchaseno,
                    null, null, barcode, adminUsername, startdate, enddate, "id", null);
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String enterpreseNo = mainform.Admin.EnterpriseNo;
                String barcode = textbox_barcode.Text.Trim();
                String goods_name = textbox_goodsname.Text.Trim();
                String category_name = textbox_categoryname.Text.Trim();
                String repurchaseno = textbox_repurchaseno.Text.Trim();
                String adminUsername = mainform.UsernameForQuery;
                String startdate = String.Empty;
                String enddate = String.Empty;
                if (dateTimePicker_startdate.Checked && dateTimePicker_enddate.Checked &&
                    dateTimePicker_enddate.Value.CompareTo(dateTimePicker_startdate.Value) >= 0)
                {
                    startdate = dateTimePicker_startdate.Value.ToShortDateString();
                    enddate = dateTimePicker_enddate.Value.ToShortDateString();
                }
                record = this.repurchaseGoodsService.GetBillRepurchaseGoodsRecord(enterpreseNo, string.Empty, repurchaseno, null, null, barcode,
                    adminUsername, startdate, enddate);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 重写事件

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Up:
                    this.dataGridView1.Focus();
                    break;
                case Keys.Down:
                    this.dataGridView1.Focus();
                    break;
                case Keys.F8:
                    this.RefreshDataGridViewData();
                    break;
            }
            return b;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        #endregion

        #region IPrinter 成员

        DataGridView IPrinter.GetAllRows()
        {
            try
            {
                ((IDataGridViewData)this).GetBindingData(0, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return this.dataGridView1;
        }

        DataGridView IPrinter.GetSelectedRows()
        {
            return this.dataGridView1;
        }

        void IPrinter.VisibleColumn(string columnname)
        {
            try
            {
                if (this.dataGridView1.Columns[columnname].Visible)
                    this.dataGridView1.Columns[columnname].Visible = false;
                else
                    this.dataGridView1.Columns[columnname].Visible = true;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion
    }
}
